<template>
  <div>
    <section>
      <div>
        <span>总营业额：{{ this.data.total }}</span>
      </div>
      <div>
        <span>销量：{{ this.data.saleCount }}</span>
      </div>
      <div>
        <span>综合评分：{{ this.data.grade }}</span>
      </div>
    </section>
    <div id="MySector"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "comRadar",
  data() {
    return {
      myChart: {},
      data: {
        total: null,
        saleCount: null,
        grade: null,
        jewCount: null,
        panCount: null,
        jaCount: null,
        shCount: null,
      },
    };
  },
  methods: {
    // 方法
    init() {
      this.myChart = echarts.init(document.getElementById("MySector"));
      window.addEventListener("resize", this.resize);
    },
    optionData() {
      let optionData = {
        title: {
          text: "商品销售额视图",
          subtext: `总额：${this.data.total}`,
          left: "center",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
        },
        series: [
          {
            name: "销售数量",
            type: "pie",
            radius: "50%",
            data: [
              { value: this.data.shCount, name: "鞋类" },
              { value: this.data.jaCount, name: "上衣" },
              { value: this.data.panCount, name: "下衣" },
              { value: this.data.jewCount, name: "饰品" },
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      this.myChart.setOption(optionData);
    },
    sizeChange() {
      this.myChart.resize();
    },
    priceTotal() {
      let url = `stroll/total?shopId=${sessionStorage.getItem("shopId")}`;
      this.axios.get(url).then((res) => {
        res.data.forEach((v) => {
          this.data.saleCount += v.count;
          this.data.total += v.price_total;
          this.data.grade += v.grade;
          if (v.type == "鞋类") this.data.shCount += v.count;
          if (v.type == "上衣") this.data.jaCount += v.count;
          if (v.type == "饰品") this.data.jewCount += v.count;
          if (v.type == "下装") this.data.panCount += v.count;
        });
        this.data.grade = (this.data.grade / res.data.length).toFixed(1);
        this.optionData();
      });
    },
  },
  mounted() {
    this.priceTotal();
    this.init();
  },
  beforeDestroy() {
    // 移除监听
    window.removeEventListener("resize", this.resize);
  },
};
</script>

<style lang="scss" scoped>
section {
  display: flex;
  justify-content: space-around;
  text-align: center;
  margin: 30px auto;
  width: 1000px;
  font-size: 20px;
  color: white;
  > div {
    width: 300px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
    border-radius: 4px;
  }
  > div:first-child {
    background-color: red;
  }
  > div:nth-child(2) {
    background-color: blue;
  }
  > div:nth-child(3) {
    background-color: rgb(139, 139, 56);
  }
}
#MySector {
  width: 500px;
  height: 500px;
  margin: 0 auto;
}
</style>